
layui.use('layer', function(){
    $(function () {
        $('input[name="user_email"]').blur(function () {
            var rege = /^.{1,20}@\w{1,10}\.\w{1,5}$/;
            var self =  $(this);
            if(!rege.test($(this).val(),rege)){
                layer.alert('不符合邮箱规范', {
                    icon: 2,
                });
                self.val('');
                return false;
            }
            var email = self.val();
            $.get(      //判断邮箱是否已经注册
                '/index/user/haveEmail?email='+email,
                function (data) {
                    if(data.code == 400){
                        layer.alert(data.msg, {
                            icon: 2,
                        });
                        self.val('');
                        return false;
                    }
                }
            );
        });

        $('input[name="user_account"]').blur(function () {
            var rege = /^\w{7,15}$/;
            var self =  $(this);
            if(!rege.test($(this).val(),rege)){
                layer.alert('账号为7-15个字符数组组成包含_', {
                    icon: 2,
                });
                self.val('');
                return false;
            }

            var account = self.val();
            $.get(      //判断账号是否已被注册
                '/index/user/haveAccount?account='+account,
                function (data) {
                    if(data.code == 400){
                        layer.alert(data.msg, {
                            icon: 2,
                        });
                        self.val('');
                        return false;
                    }
                }
            );
        });

        $('input[name="user_nickname"]').blur(function () {
            var rege = /^.{1,15}$/;
            var self =  $(this);
            if(!rege.test($(this).val(),rege)){
                layer.alert('昵称长度为1-15个任何字符', {
                    icon: 2,
                });
                self.val('');
                return false;
            }

            var nickname = self.val();
            $.get(      //判断账号是否已被注册
                '/index/user/haveNickname?nickname='+nickname,
                function (data) {
                    if(data.code == 400){
                        layer.alert(data.msg, {
                            icon: 2,
                        });
                        self.val('');
                        return false;
                    }
                }
            );
        });

        $('input[name="user_pwd"]').blur(function () {
            var rege = /^.{10,15}$/;
            var self =  $(this);

            if(!rege.test($(this).val(),rege)){
                layer.alert('密码长度为10-15个数字或字符包括_', {
                    icon: 2,
                });
                self.val('');
                return false;
            }
        });

        $('input[name="check_pwd"]').blur(function () {
            var self =  $(this);
            var pwd =  $('input[name="user_pwd"]').val();

            if(self.val() != pwd){
                layer.alert('两次密码输入不一致', {
                    icon: 2,
                });
                self.val('');
                return false;
            }
        });


        function notNull(name,msg)
        {
            var user_label = $('input[name="'+name+'"]').val();
            var self =  $(this);
            if(user_label == ''){
                layer.msg(msg);
                self.val('');
                return false;
            }
            return true;
        }



        $('#test').click(function () {      //表单提交事件

            var check1 = notNull('user_email','邮箱不能为空！');

            var check2 = notNull('user_account','账户不能为空！');

            var check3 = notNull('user_nickname','昵称不能为空！');

            var check4 = notNull('user_pwd','密码不能为空！');

            var check5 = notNull('emailCheck','验证码不能为空！');

            var check6 = notNull('check_pwd','确认密码栏！');


            if(check1&&check2&&check3&&check4&&check5&&check6){    //判断是否为空
                var user_email = $('input[name="user_email"]').val();
                var user_account = $('input[name="user_account"]').val();
                var user_nickname = $('input[name="user_nickname"]').val();
                var user_pwd = $('input[name="user_pwd"]').val();
                var check_pwd = $('input[name="check_pwd"]').val();
                var emailCheck = $('input[name="emailCheck"]').val();

                $.post(
                    'register',
                    {
                        user_email:user_email,
                        user_account:user_account,
                        user_nickname:user_nickname,
                        user_pwd:user_pwd,
                        emailCheck:emailCheck
                    },
                    function (data) {
                        alert(data.msg);
                        if(data.code == 200){
                            location.href='login';
                        }
                    }
                );

            }else {
              return false;

            }

            return false;

        });




        $('#code').click(function () {
            var user_email = $('input[name="user_email"]').val();
            var self = $(this);
            if(user_email == ''){
                layer.alert('请输入邮箱', {
                    icon: 2,
                });
                return false;
            }

            $.get(
                'sendEmailCode?email='+user_email,
                function (data) {
                    if(data.code == 200){       //如果邮件发送成功
                        layer.alert(data.msg, {
                            icon: 1,
                        });
                        self.attr('disabled','disabled');   //按钮设置为禁用
                        var count = $("#code_phone_count");
                        var num = count.text();     //获取总秒数

                        var time =
                            setInterval(function () {       //设置定时器
                                num --;

                                count.text(num);
                                if(num == 0){
                                    clearInterval(time);
                                    self.removeAttr('disabled');//按钮设置为不禁用
                                    count.text(60);
                                                    }
                                },1000);
                    }else {
                        layer.alert('验证码发送失败请重试', {
                            icon: 2,
                        });
                    }


                }
            );

        })
    })
});

